// pages/shop/detail.js
var util = require('../../utils/util.js');

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgWidth: 0,
    address:{},
    cart:{},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.setData({

      imgWidth: app.globalData.imgWidth,
    });
    wx.getStorage({
      key: 'cart',
      success: function (res) {

        that.setData({
          cart: res.data
        })
      },

    })
    var data = {};
    var id = options.id ? options.id : 0;
    data.openid = wx.getStorageSync("openid");
    data.session = wx.getStorageSync("session");
    data.id = id;
    util.request('/api-user/get-address-by-id', data, function (data) {
      that.setData({

        address: data.data,
      })
    });

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  addOrder:function(){
    var data={};
    data.openid = wx.getStorageSync("openid");
    data.session = wx.getStorageSync("session");
    data.cart = this.data.cart;
    data.address = this.data.address.id;
    util.request('/api-user/add-order', data, function (data) {
      if(data.status){
        wx.removeStorageSync("cart");
        wx.redirectTo({
          url: '../order/list',
        })
      }else{
        wx.showToast({
          title: data.msg,
          icon: none,
          duration: 2000
        })
      }
    });
  }
})